#include <cstdio>
#include <cmath>
#include <iostream>
#include <fstream>
#include <vector>
#include "ImpliedVolBisection.h"
#include "MatrixRegression.h"

using namespace std;

void printDebug(vector<double> stks, vector<double> prs, vector<double> sis);

int main()
{
	double Price = 1177;
	double Strike;
	double Tdays;
	double RiskFree = .026;
	double DividendRate = .018;
	double OptionPrice;
	int size;

    // Read Option Data from File(Time in Days, Markets Prices, Strikes)
    // Stores these values in vectors.
 	FILE* regression_file;
	regression_file = fopen("optionsdatacomma2.csv", "r");
	
	vector<double> timeindays;
	vector<double> prices;
	vector<double> strikes;

	while (!feof(regression_file)) {
		double t;
        double s;
    	double k;

		fscanf(regression_file, "%lf %lf %lf", &t, &k, &s);
        timeindays.push_back(t);
        strikes.push_back(k);
        prices.push_back(s);
	}
    
	fclose(regression_file);
    
    size = prices.size()-1;
    vector<double> sigmas(size);
	
	/*
     * For each option visible in the market,
	 * we "back out" a sigma from the Black-Scholes formula.
	 * 
     * Uses the bisection method.         
	 */
	for (int m = 0; m < size; m++) {
	    OptionPrice = prices[m];
	    Strike = strikes[m];
	    Tdays = timeindays[m];
	    
	    sigmas[m] = FindImpliedVol(CALL, Price, Strike, Tdays/365,
								   RiskFree, DividendRate, OptionPrice);
    } 

	// If you want to print those implied volatility, delete the //
	// printDebug(strikes, prices, sigmas);

    // Regression
    Regression(Price, strikes, timeindays, sigmas);    
}

void printDebug(vector<double> stks, vector<double> prs, vector<double> sis)
{
	for (int i = 0; i < stks.size(); i++)
		cout << stks[i] << " " << prs[i] << " " << sis[i] << endl;
}
